package com.learn.exp;

import com.alibaba.csp.sentinel.Tracer;
import com.learn.resp.Result;
import com.learn.resp.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author DJJ_F
 * @version 1.0
 * @date 2025/07/30
 * @description
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<String> exception(Exception e) {
        log.error("全局异常信息exception:{}", e.getMessage(), e);
        // 让sentinel跟踪产生的异常，使熔断规则等能生效
        Tracer.trace(e);
        return Result.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
    }
}
